package com.legolas.provider1.api.serviceapi;

import com.alibaba.dubbo.config.annotation.Service;
import com.legolas.api.service.SeckillService;
import com.legolas.provider1.dao.GoodsDao;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/8/8 0008
 */
@Service(version = "1.0.0", interfaceClass = com.legolas.api.service.SeckillService.class, timeout = 2000)
public class SeckillServiceAPI implements SeckillService {


    @Autowired
    GoodsDao goodsDao;

    @Override
    public int addGoods(String name, Integer stock) {
        return goodsDao.addGoods(name, stock);
    }

    @Override
    public int buyGoods(Integer id) {
        int stock = goodsDao.queryStockById(id);
        if (stock <= 0) {
            return -1;
        } else {
            return goodsDao.minusStock(id);
        }
    }

    @Override
    public int addStock(Integer id) {
        try {
            goodsDao.addStock(id);
            return goodsDao.queryStockById(id);
        } catch (Exception e) {
            return -1;
        }
    }

    @Override
    public List getAllGoods() {
        return goodsDao.listGoods();
    }


}
